package com.xiaoyuzhou;

import java.io.InputStream;

import com.xiaoyuzhou.R;
import com.xiaoyuzhou.utils.NetUtils;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.ImageView;

public class MainActivity extends Activity{
	
	ImageView imgv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_main);
		imgv = (ImageView) findViewById(R.id.main_imgv);
		ImageTask task = new ImageTask();
		task.execute();
	}

	Handler handler = new Handler(Looper.getMainLooper()) {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				BitmapDrawable bd = (BitmapDrawable) msg.obj;
				imgv.setImageDrawable(bd);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	class ImageTask extends AsyncTask<String, Integer, Object> {

		@Override
		protected Object doInBackground(String... params) {
			InputStream is = NetUtils.getInputStream("http://www.ggdown.com/data/qr/5029.png");
			BitmapDrawable bd = new BitmapDrawable(is);
			Message msg = new Message();
			msg.what = 1;
			msg.obj = bd;
			handler.sendMessage(msg);
			return bd;
		}
		
	}
	
}
